package com.csust.automaticteach.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.annotation.AuthCheck;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.constant.UserAuthConstant;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.promptself.PromptSelfAddRequest;
import com.csust.automaticteach.model.dto.promptself.PromptSelfQueryRequest;
import com.csust.automaticteach.model.dto.promptself.PromptSelfUpdateRequest;
import com.csust.automaticteach.model.entity.PromptSelf;
import com.csust.automaticteach.model.vo.promptself.PromptSelfVO;
import com.csust.automaticteach.service.PromptSelfService;
import com.csust.opration.logger.annotation.LogOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import static com.csust.automaticteach.constant.SectionConstant.LEARN_ASSISTANT;
import static com.csust.automaticteach.constant.SectionConstant.PROMPT_MANAGE;
import static com.csust.opration.logger.constant.CommonConstant.*;

/**
 * @ClassNamePromptSelfController
 * @Description 提示词控制器
 * @Author钰玟
 * @Date2025/7/10 下午8:30
 * @Version 1.0
 **/
@RestController
@RequestMapping("/promptSelf")
public class PromptSelfController {

    @Resource
    private PromptSelfService promptSelfService;

    /**
     * 用户端分页查询prompt
     * @param promptSelfQueryRequest 查询条件
     * @param request 请求
     * @return  提示词列表
     */
    @PostMapping("/list/page/vo")
    @LogOperation(menu = LEARN_ASSISTANT, type = VIEW, content = "'学生获取提示词:'+{#promptSelfQueryRequest.searchText}")
    public BaseResponse<Page<PromptSelfVO>> listPromptSelfVOByPage(@RequestBody PromptSelfQueryRequest promptSelfQueryRequest,
                                                                   HttpServletRequest request) {
        ThrowUtils.throwIf(promptSelfQueryRequest == null, ErrorCode.PARAMS_ERROR);
        //获取分页数据 current and pageSize
        int current = promptSelfQueryRequest.getCurrent();
        int pageSize = promptSelfQueryRequest.getPageSize();
        //得到实体类的page对象
        Page<PromptSelf> page = promptSelfService.page(new Page<>(current, pageSize), promptSelfService.getPromptSelfVOQueryWrapper(promptSelfQueryRequest));
        Page<PromptSelfVO> promptSelfVOPage = new Page<>(current, pageSize, page.getTotal());
        List<PromptSelfVO> promptSelfVOList = promptSelfService.getPromptSelfVOList(page.getRecords());
        promptSelfVOPage.setRecords(promptSelfVOList);
        return ResultUtils.success(promptSelfVOPage);
    }

    /**
     * 管理端分页查询prompt
     * @param promptSelfQueryRequest 查询条件
     * @param request 请求
     * @return  提示词列表
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Page<PromptSelf>> listPromptSelfByPage(@RequestBody PromptSelfQueryRequest promptSelfQueryRequest,
                                                                   HttpServletRequest request) {
        ThrowUtils.throwIf(promptSelfQueryRequest == null, ErrorCode.PARAMS_ERROR);
        //获取分页数据 current and pageSize
        int current = promptSelfQueryRequest.getCurrent();
        int pageSize = promptSelfQueryRequest.getPageSize();
        //得到实体类的page对象
        Page<PromptSelf> page = promptSelfService.page(new Page<>(current, pageSize)
                ,promptSelfService.getPromptSelfQueryWrapper(promptSelfQueryRequest));
        return ResultUtils.success(page);
    }


    /**
     *添加新的prompt
     * @param promptSelfAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @LogOperation(menu = PROMPT_MANAGE, type = VIEW, content = "'管理增加提示词:'+{#promptSelfAddRequest.role}")
    public BaseResponse<Long> addPromptSelf(@RequestBody PromptSelfAddRequest promptSelfAddRequest) {
        ThrowUtils.throwIf(promptSelfAddRequest == null, ErrorCode.PARAMS_ERROR);
        PromptSelf promptSelf = new PromptSelf();
        BeanUtil.copyProperties(promptSelfAddRequest, promptSelf);
        boolean b = promptSelfService.saveOrUpdate(promptSelf);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(promptSelf.getId());
    }

    /**
     *删除 prompt
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @LogOperation(menu = PROMPT_MANAGE, type = DELETE, content = "'管理删除提示词:'+{#deleteRequest.id}")
    public BaseResponse<Boolean> deletePromptSelf(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean b = promptSelfService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     *修改 prompt
     * @param promptSelfUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @LogOperation(menu = PROMPT_MANAGE, type = UPDATE, content = "'管理删除提示词:'+{#promptSelfUpdateRequest.role}")
    public BaseResponse<Boolean> updatePromptSelf(@RequestBody PromptSelfUpdateRequest promptSelfUpdateRequest) {
        ThrowUtils.throwIf(promptSelfUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        PromptSelf promptSelf = new PromptSelf();
        BeanUtil.copyProperties(promptSelfUpdateRequest, promptSelf);
        promptSelf.setUpdateTime(new Date());
        boolean b = promptSelfService.saveOrUpdate(promptSelf);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
    /**
     *获取prompt
     * @param id  id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @LogOperation(menu = PROMPT_MANAGE, type = VIEW, content = "'管理获取提示词详情:'+{#id}")
    public BaseResponse<PromptSelf> getPromptSelf(@RequestParam("id") Long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        PromptSelf promptSelf = promptSelfService.getById(id);
        return ResultUtils.success(promptSelf);
    }

}
